import { PlusOutlined } from '@ant-design/icons';
import { Button, Divider, message, Modal, Drawer } from 'antd';
import React, {useState, useRef, useEffect} from 'react';
import { PageContainer, FooterToolbar } from '@ant-design/pro-layout';
import ProTable from '@ant-design/pro-table';
import ProDescriptions from '@ant-design/pro-descriptions';
import { queryRule } from './service';
import {history} from "umi";
import request from "@/utils/request";


const TableList = () => {
  const actionRef = useRef();
  const [isSync, setIsSync] = useState(false);

  const columns = [
    {
      title: '矿工号',
      dataIndex: 'MinerId',
    },
    {
      title: '日期',
      dataIndex: 'TimeString',
      search: false,
    },
    {
      title: '出块权',
      dataIndex: 'RoundWinner',
      search: false,
      render: (text) => {
        if (text < 0) {
          return "-"
        }
        return text
      },
    },
    {
      title: '出块数',
      dataIndex: 'RewardCount',
      search: false,
    },
    {
      title: '赢块率',
      dataIndex: 'WinPerDay',
      search: false,
      render: (text) => {
        return text.toFixed(2)
      }
    },
    {
      title: '幸运值',
      dataIndex: 'Lucky',
      search: false,
      render: (text) => {
        return text.toFixed(2)
      }
    },
    {
      title: '出块奖励',
      dataIndex: 'RewardFil',
      search: false,
      render: (text) => {
        return text.toFixed(2)
      }
    },
    {
      title: '出块收益',
      dataIndex: 'EveryTibEarnings',
      search: false,
      render: (text) => {
        return text.toFixed(2)
      }
    },
    {
      title: '释放FIL',
      dataIndex: 'ReleaseFil',
      search: false,
      render: (text) => {
        return text.toFixed(2)
      }
    },
    {
      title: '立即释放',
      dataIndex: 'ImmediateRelease',
      search: false,
      render: (text) => {
        return text.toFixed(2)
      }
    },
    {
      title: '线性释放',
      dataIndex: 'Vesting',
      search: false,
      render: (text) => {
        return text.toFixed(2)
      }
    },
    {
      title: '开始日期',
      dataIndex: 'StartTime',
      hideInTable: true,
      valueType:'date',
    },
    {
      title: '结束日期',
      dataIndex: 'EndTime',
      hideInTable: true,
      valueType:'date',
    },
  ];

  const request2 = async(
      // 第一个参数 params 查询表单和 params 参数的结合
      // 第一个参数中一定会有 pageSize 和  current ，这两个参数是 antd 的规范
      params ,
      sorter,
      filter,
    ) => {
    // 这里需要返回一个 Promise,在返回之前你可以进行数据转化
    // 如果需要转化参数可以在这里进行修改
    console.log(params)

    const result = await queryRule({...params,sorter,filter});
    if (result.Code != 0) {
      message.error(result.Msg)
      return
    }
    console.log(result.Data.Data)
    setIsSync(result.Data.IsSync)
    return {
      data:result.Data.Data,
      success:result.Code === 0,
      total:result.Data.Total,
    };
  }


  return (
    <PageContainer>
      <ProTable
        actionRef={actionRef}
        // rowKey="MinerId"
        search={{
          defaultCollapsed: false,
          optionRender: ({ searchText, resetText }, { form }) => [
            <Button
              key="search"
              type="primary"
              onClick={() => {
                form?.submit();
              }}
            >
              {searchText}
            </Button>,
            <Button
              key="rest"
              onClick={() => {
                form?.resetFields();
              }}
            >
              {resetText}
            </Button>,
            <Button key="out"
                    onClick={() => {

                      const sendData = {
                        MinerId:form.getFieldValue("MinerId"),
                        StartTime:form.getFieldValue("StartTime") ? form.getFieldValue("StartTime").format('YYYY-MM-DD'):"",
                        EndTime:form.getFieldValue("EndTime") ?form.getFieldValue("EndTime").format('YYYY-MM-DD'):"",
                      }

                      var str='?';
                      for(var i in sendData){
                        if (sendData[i] != "" && sendData[i]) {
                          str+= i+'='+sendData[i]+"&";
                        }
                      }

                      window.location.href=`/api/miner/incomeExport${str}`

                    }}
            >导出</Button>,
          ],
        }}
        options={false}
        // toolBarRender={() => [
        //   <Button type="primary">
        //     {isSync?'统计中':'统计完成'}
        //   </Button>,
        // ]}
        // toolBarRender={false}
        // request={(params, sorter, filter) => queryRule({ ...params, sorter, filter })}

        request={request2}
        columns={columns}
      />

    </PageContainer>
  );
};

export default TableList;
